<?php
class Admin extends CI_Controller 
{
    public function __construct() {
        parent::__construct();
        $this->load->model('products_model');
        $this->load->model('categories_model');
    }
    public function index() {
//        if (!$this->session->userdata('validated')) {
//            redirect('admin/login');
//        }
        //TODO
        $data = array(
            'title' => 'Products',
            'products' => $this->products_model->get_where_products(NULL,$counter=FALSE),
            'total' => $this->products_model->get_where_products(NULL,$counter=true),
            'categories'=>$this->categories_model->get_categories(),
            'controller'=>   $this->router->fetch_class(),
            'action' =>      $this->router->fetch_method()
        );
        $this->load->view('admin/templates/layout', $data);
    }
    
    public function  news(){
        $this->load->library('pagination');
        $this->load->model('news_model');
         $data = array(
            'title' => 'News',
            'news' => $this->news_model->get_news(NULL,$counter=FALSE),
            'total' => $this->news_model->get_news(NULL,$counter=true),
            'controller'=>   $this->router->fetch_class(),
            'action' =>      $this->router->fetch_method()
        );
        $this->load->view('admin/templates/layout', $data);
        
    }

    public function categories() {
//         if (!$this->session->userdata('validated')) {
//            redirect('admin/login');
//        }
         $data = array(
            'title' => 'Categories',
            'categories' => $this->categories_model->get_categories(NULL,FALSE),
            'total' => $this->categories_model->get_categories(NULL,TRUE),
            'controller'=>   $this->router->fetch_class(),
            'action' =>      $this->router->fetch_method()
        );
        $this->load->view('admin/templates/layout',$data);
    }
    public function login() {
        $data = array(
            'title' => 'Page-Keywors',
            'controller'=>   $this->router->fetch_class(),
            'action' =>      $this->router->fetch_method()
        );
        $this->load->view('admin/templates/layout', $data);
    }
    public function logout() {
        $this->session->sess_destroy();
        $this->login();
    }

    public function process(){
        // Load the model
        $this->load->model('login_model');
        // Validate the user can login
        $result = $this->login_model->validate();
        var_dump($result);
        // Now we verify the result
        if(! $result){
            $this->login();
        }else{
            redirect('admin');
        }        
    }
    public function set_categories() {
        $this->load->model('Sites_model');
        $this->load->model('categories_model');
        $this->load->model('all_categories_model');
        $array_segments=array(
            'site'=>0,
        );
        for($i=3;$i<$this->uri->total_segments();$i+=2){
         $array_segments[$this->uri->segment($i)]=  $this->uri->segment($i+1)?$this->uri->segment($i+1):NULL; 
        }
        if(!empty($_POST['categories'])){
        for($i=0; $i<count($_POST['categories']); $i++){
           if(!empty($_POST['ref'][$i]))
                $this->all_categories_model->update_all_category(array('id_category'=>$_POST['ref'][$i]),array('id'=>$_POST['categories'][$i]));    
        }
        }
        $data = array(
            'title' => 'Page-Keywors',
            'sites'=>$this->Sites_model->get_sites(),
            'array_segments'=>$array_segments,
            'categories'=>$this->categories_model->get_categories(),
            'all_categories'=>$this->all_categories_model->get_all_category_where($array_segments),
            'controller'=>   $this->router->fetch_class(),
            'action' =>      $this->router->fetch_method()
        );
        $this->load->view('admin/set_categories', $data);
    }
}